<?php

if (!defined('__BACKEND__'))
    die("you cannot directly access this page");
/*
 * by SONY K Wibisono
 * e-mail : sony@sonywibisono.com
 */

//Place code here
function delete(){
   global $db,$id;
    
//    $title=get_event_info($id, $info)
    $msg= "Event was deleted!";
    $db->query("delete from _event_subscriber where event_id=$id");
    $db->query("delete from _event_index where event_id=$id");
    $db->query("delete from _event_archives where event_id=$id");
    $db->query("delete from _event_events where id=$id");
    refresh_to("?mod=events#", $msg, 2);
}
function event_subscriber_count($event_id){
    global $db;
    $sql="select count(*) as jumlah from _event_subscriber where event_id=$event_id and event_granted='1'";
    $row=$db->fetch_row($sql);
    if (!empty($row)){
        return $row[0];
    } else return 0;
}
function list_table(){
    global $db,$page;
    $sql="select a.*,b.category_name from _event_events a,_event_category b 
        where a.event_category=b.id order by a.id asc";
        $result=$db->query($sql);
        
    echo "<div align=\"right\"><a href=\"?mod=panel\"><img class=\"icon\" src=\"images/panel.png\" title=\"Back to Panel\"/></a>
<a href=\"?mod=events&action=add\"><img class=\"icon\" src=\"images/panel/add_event.png\" title=\"Add events\"/></a></div>        
<table id=\"event_list\" cellspacing=0 cellpadding=2>
<tr><th>No</th><th>Title</th><th>Schedule</th><th>Place</th><th width=\"60\">Subscriber</th></tr>";
    //;
    //for($i=0;$i<count($row);$i++){
    $i=0;
    while ($row=$db->fetch_array($result)){  
    extract($row);
    $i++;
    $time_start=explode(" ",$event_time_start);
    $time_finish=explode(" ",$event_time_finish);
    $memcount=event_subscriber_count($id);
    echo "<tr><td class=\"table_row_left\">$i</td>
    <td class=\"table_row\"><a id=\"event_$id\" href=\"#\">$event_title</a>
    <div id=\"event_action_$id\">
    <a href=\"?mod=events&file=archive&action=list&id=$id\">
    <img class=icon src=\"images/ic_archive.png\" title=\"List Archives\" /></a>
    <a href=\"?mod=panel&file=events&action=subscriber&id=$id\">
    <img class=icon src=\"images/subscriber_grant.png\" title=\"Subscriber Grant\" /></a>
    <a href=\"?mod=events&action=edit&id=$id\">
    <img class=icon src=\"images/edit.png\" title=\"Edit\" /></a>
    <a class=\"delete_confirm\" href=\"?mod=panel&file=events&action=delete&id=$id\">
    <img class=icon src=\"images/delete.png\" title=\"Hapus\" /></a>
    </div>
    </td>
    <td class=\"table_row\">$event_day at $time_start[1] until $time_finish[1]</td>
    <td class=\"table_row\">$event_venue</td>
    <td class=\"table_row\"><p align=right>$memcount</p> </td></tr>
    <script>\r\n
    $('#event_action_$id').hide();
    $('#event_$id').mouseover(function(){
    $('#event_action_$id').show();
    });
    $('#event_$id').mouseup(function(){
    $('#event_action_$id').hide();
    });
    </script>";
    }
    echo "</table>";
}
function subscriber_grant(){
    global $db,$cat,$id,$login_status;
    if (!empty($id)){ 
        $event_title=get_event_info($id, 'event_title');
        $sql="select b.fullname,a.*
        from _event_subscriber a,_user_users b 
        where a.event_id=$id and a.event_member=b.id";
        $result=$db->query($sql);
        $rownum=mysql_num_rows($result);
        if (($rownum>0)) {
        $granted=array('not_ok.png','ok.png');
        $statusnya=array('Ditolak','Diterima');
        echo "
<h3>Granting for $event_title Event</h3>    
        <a href=\"?mod=panel&file=events\">Back to Event List</a>
<table width=\"100%\" cellspacing=0><tr><th>Subscriber</th><th>Vicon IP</th>
            <th>Contact</th><th>Status</th><th>Action</th></tr>";
        while($row=$db->fetch_array($result)){
            extract($row);
            $image_status=$granted[$event_granted];
            $status=$statusnya[$event_granted];
            if ($event_granted=='1'){
                $action="<a href=\"?mod=panel&file=events&action=revoke&member_id=$event_member&event_id=$event_id\">
    <img class=icon src=\"images/not_ok.png\" title=\"Ditolak\" /></a>";
            } else {
              $action="<a href=\"?mod=panel&file=events&action=grant&member_id=$event_member&event_id=$event_id\">
    <img class=icon src=\"images/ok.png\" title=\"Diterima\" /></a>";  
            }
            if ($event_member=='2'){
                $fullname="Guest, $member_email";
            }
            echo "<tr><td class=\"table_row_left\">$event_institution</td>
            <td class=\"table_row\">$event_vicon_ip</td>
            <td class=\"table_row\">$fullname</td>
            <td class=\"table_row\"><center><img class=icon src=\"images/$image_status\" title=\"$status\"/></center></td>
            <td  class=\"table_row\"><p align=center>$action</p>
            </td></tr>";
        }
        echo "</table>";
        } else {
            echo "Tidak ada member pada event : <b>$event_title</b>";
        }
    }
    
}
function grant_member(){
    global $db,$member_id,$event_id;
    $sql="update _event_subscriber set event_granted='1' where event_member=$member_id and event_id=$event_id";
    $db->query($sql);
    refresh_to("?mod=panel&file=events&action=subscriber&id=$event_id", "Kembali ke daftar Pendaftar", 2);
}
function revoke_member(){
    global $db,$member_id,$event_id;
    $sql="update _event_subscriber set event_granted='0' where event_member=$member_id and event_id=$event_id";
    $db->query($sql);
    refresh_to("?mod=panel&file=events&action=subscriber&id=$event_id", "Kembali ke daftar Pendaftar", 2);
}
switch($action){
    case 'subscriber':
        subscriber_grant($id);
        break;
    case 'delete':
        delete();
        break;
    case 'grant':
        grant_member();
        break;
    case 'revoke':
        revoke_member();
        break;
    default:
        list_table();
        break;
}
?>
<div id="dialog" title="Confirmation">
  Are you sure to delete this Event?
</div>
<script type="text/javascript">
  $(document).ready(function() {
    $("#dialog").dialog({
      autoOpen: false,
      modal: true
    });
  });

  $(".delete_confirm").click(function(e) {
    e.preventDefault();
    var targetUrl = $(this).attr("href");

    $("#dialog").dialog({
      buttons : {
        "Confirm" : function() {
          window.location.href = targetUrl;
        },
        "Cancel" : function() {
          $(this).dialog("close");
        }
      }
    });

    $("#dialog").dialog("open");
  });
</script>